﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyManager.Business
{
    public class TaskManager
    {
        private ITaskDataManager _taskDataManager;
        private DateTime _currentDateTime;

        public TaskManager(ITaskDataManager taskDataManager, DateTime currentDateTime)
        {
            this._taskDataManager = taskDataManager;
            this._currentDateTime = currentDateTime;
        }

        #region "Public Methods"

        public Task Create(Task task)
        {

            return this._taskDataManager.Create(task);
        }

        public int Create(TaskCollection tasks)
        {
            //TODO: refine the status returning logic
            int status = 1;
            foreach (Task task in tasks)
            {
                this._taskDataManager.Create(task);
                status = 1;
            }

            return status;
        }

        public  int Create(string title, DateTime dueDate)
        {
            throw new NotImplementedException();
        }

        public int Create(string title)
        {
            throw new NotImplementedException();
        }

        
        public  Task Update(Task task)
        {
            return this._taskDataManager.Update(task);
        }

        public int Update(TaskCollection tasks)
        {
            //TODO: refine the status returning logic
            int status = 1;
            foreach (Task task in tasks)
            {
                this._taskDataManager.Update(task);
                status = 1;
            }

            return status;
        }

        public Task UpdateTaskAsCompleted(Task task)
        {
            task.Status = TaskStatus.Completed;
            return this._taskDataManager.Update(task);

        }

        public int UpdateTasksAsCompleted(TaskCollection tasks)
        {
            //TODO: refine the status returning logic
            int status = 1;
            foreach (Task task in tasks)
            {
                task.Status = TaskStatus.Completed;
                this._taskDataManager.Update(task);
                status = 1;
            }

            return status;

        }

        public int Delete(string taskId)
        {
            return this._taskDataManager.Delete(taskId);
        }
        
        public int Delete(Task task)
        {
            return this._taskDataManager.Delete(task.Id);
        }

        public int Delete(TaskCollection tasks)
        {
            //TODO: refine the status returning logic
            int status = 1;
            foreach (Task task in tasks)
            {
                status = this._taskDataManager.Delete(task.Id);
            }

            return status;
        }
        

        #region "Retrieval Methods"

        public Task GetTaskById(string taskId)
        {
            return this._taskDataManager.GetTaskById(taskId);
        }


        public TaskCollection GetTasksByCategory(string categoryId)
        {
            StringCollection tempCollection = new StringCollection();
            tempCollection.Add(categoryId);
            return this._taskDataManager.GetTasksByCategoryIds(tempCollection);
        }


        public TaskCollection GetTasksByCategory(Category category)
        {
            StringCollection tempCollection = new StringCollection();
            tempCollection.Add(category.Id);
            return this._taskDataManager.GetTasksByCategoryIds(tempCollection);
        }


        public TaskCollection GetTasksByCategories(CategoryCollection categories)
        {
            return this._taskDataManager.GetTasksByCategoryIds(categories.GetCategoryIds());
        }       

        public TaskCollection GetTasksByCategoryIds(StringCollection categoryIds)
        {
            return this._taskDataManager.GetTasksByCategoryIds(categoryIds);
        }

        public TaskCollection GetTasksByDateRange(DateTime fromDate, DateTime toDate)
        {
            return this._taskDataManager.GetTasksByDateRange(fromDate, toDate);
        }

        public TaskCollection GetTasksForOneWeek()
        {
            return this._taskDataManager.GetTasksByDateRange(_currentDateTime, _currentDateTime.AddDays(7));
        }

        public TaskCollection GetTasksForToday()
        {
            return this._taskDataManager.GetTasksByDateRange(_currentDateTime, _currentDateTime);

        }

        public TaskCollection GetTasksForTomorrow()
        {
            return this._taskDataManager.GetTasksByDateRange(_currentDateTime.AddDays(1), _currentDateTime.AddDays(1));
        }

        public TaskCollection GetTasksForOneMonth()
        {
            return this._taskDataManager.GetTasksByDateRange(_currentDateTime, _currentDateTime.AddMonths(1));
                       
        }

        //Tasks are considered InProgress by default if StartDate is a past date and DueDate is a Future date( this means the Task has been started already)
        public TaskCollection GetInProgressTasks()
        {
            return this._taskDataManager.GetInProgressTasks(this._currentDateTime);
        }

        public  TaskCollection GetOverDueTasks()
        {
         this._taskDataManager.GetTasksByDateRange(_currentDateTime.AddDays(1), _currentDateTime.AddDays(1));

            return this._taskDataManager.GetOverDueTasks(this._currentDateTime);
        }

        public TaskCollection GetTasksByStatus(TaskStatus status)
        {
            return this._taskDataManager.GetTasksByStatus(status);
        }

        public TaskCollection GetOpenTasks()
        {
            return this._taskDataManager.GetTasksByStatus(TaskStatus.Open);
        }

        public TaskCollection GetCompletedTasks()
        {
            return this._taskDataManager.GetTasksByStatus(TaskStatus.Completed);
        }

        public TaskCollection GetStoppedTasks()
        {
            return this._taskDataManager.GetTasksByStatus(TaskStatus.Stopped);
        }

        public TaskCollection GetCancelledTasks()
        {
            return this._taskDataManager.GetTasksByStatus(TaskStatus.Cancelled);
        }



    
        //TODO: methods to be implemented for Mode

        #endregion 
        #endregion
    
    }
}
